package com.nowcoder.community.service;

import com.nowcoder.community.dao.DiscussPostMapper;
import com.nowcoder.community.entity.DiscussPost;
import com.nowcoder.community.util.HostHolder;
import com.nowcoder.community.util.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

@Service
public class DiscussPostService {
    @Autowired
    private DiscussPostMapper discussPostMapper;

    @Autowired
    private SensitiveFilter sensitiveFilter;

    @Autowired
    private HostHolder hostHolder;
    public List<DiscussPost> findDiscussPosts(int id, int offset, int limit) {
        return discussPostMapper.selectDiscussPosts(id,offset,limit);
    }

    public int findDiscussPostCount(int userId) {

        return discussPostMapper.selectDiscussPostRows(userId);

    }

    public int addDiscussPost(DiscussPost discussPost) {
        if (discussPost==null){
            return 0;
        }
        discussPost.setTitle(sensitiveFilter.filter(HtmlUtils.htmlEscape(discussPost.getTitle())));
        discussPost.setContent(sensitiveFilter.filter(HtmlUtils.htmlEscape(discussPost.getContent())));


        return discussPostMapper.insertDiscussPost(discussPost);
    }
}
